import localStorage from "@/utils/localStorage"

export const isProduction = () => process.env.NODE_ENV === "production"

// 返回node样式的错误.[err, null]这种
export const nodeErrorPromise = (pro) =>
  Promise.resolve(pro)
    .then((result) => [null, result])
    .catch((err) => [err, null])

// 返回初始的token
export const getToken = () => {
  const db = localStorage(true)
  return db.get("current-token")
}

// 返回退出的url
export const getReturnUrl = () => {
  const db = localStorage(true)
  return window.$configUrl?.baseUrl ?? db.get("returnUrl")
}
export const getUserName = () => {
  const db = localStorage(true)
  return db.get("user-name")
}

// 接收columns，返回需要sort排序的字段，注意columns需要传递sortKey，因为后端每一项的都不同，例如
/*
  {
    title: '人数',
    dataIndex: 'userNum',
    sorter: true,
    sortOrder: false,
    sortKey: 'user_num'
  }
*/
export const sortTransformation = (columns) => {
  const order = Object.values(columns).find(
    (f) => f.sortOrder !== false && "sortOrder" in f
  )
  if (!order) {
    return undefined
  }
  const { sortKey, sortOrder } = order

  return {
    order: sortOrder !== "descend",
    sort: sortOrder ? sortKey : "",
  }
}
